
package com.ikamobile.flickrer;

import org.json.JSONException;

import android.app.Activity;
import android.app.SearchManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

public class PhotoGridActivity extends Activity {
    protected static final String LOG_TAG = PhotoGridActivity.class.getSimpleName();
    private static final String EXRA_POSITION = "Position";
    private GridView mPhotoGrid;
    private View mProgressBar;
    private List<Photo> mPhotos;
    private int mPosition;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_grid);
        mPhotoGrid = (GridView) findViewById(R.id.photo_grid);
        mProgressBar = findViewById(R.id.progress);
        if (savedInstanceState != null)
            mPosition = savedInstanceState.getInt(EXRA_POSITION);

        populate();
    }

    private void populate() {
        final String keywords = getIntent().getStringExtra(SearchManager.QUERY);
        mProgressBar.setVisibility(View.VISIBLE);
        /*
         * Only works in main thread
         */
        mPhotos = (List<Photo>) getLastNonConfigurationInstance();
        new AsyncTask<Void, Void, List<Photo>>() {
            @Override
            protected List<Photo> doInBackground(Void... params) {
                try {
                    if (mPhotos == null)
                        mPhotos = FlickrApi.getPhotos(keywords);
                    return mPhotos;
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Failed to fetch photos, keyword=" + keywords, e);
                    return null;
                } catch (JSONException e) {
                    Log.e(LOG_TAG, "Failed to fetch photos, keyword=" + keywords, e);
                    return null;
                }
            }

            protected void onPostExecute(List<Photo> photos) {
                mProgressBar.setVisibility(View.GONE);
                if (photos != null) {
                    PhotoAdapter adapter = new PhotoAdapter(PhotoGridActivity.this, photos);
                    mPhotoGrid.setAdapter(adapter);
                    new Handler().post(new Runnable() {
                        public void run() {
                            mPhotoGrid.setSelection(mPosition);
                        }
                    });
                } else
                    Toast.makeText(PhotoGridActivity.this, "Failed to download photos.",
                            Toast.LENGTH_LONG);
            };

        }.execute();
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mPhotos;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        int position = mPhotoGrid.getFirstVisiblePosition();
        outState.putInt(EXRA_POSITION, position);
    }

}
